<template>
  <div class="student-home">
    <el-card class="box-card">
      <h2 class="student-title">个人信息</h2>
      <el-form :model="studentInfo" label-width="120px" class="student-info-form">
        <el-form-item label="学号">
          <el-input v-model="studentInfo.studentId" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="studentInfo.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="studentInfo.gender" disabled></el-input>
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="studentInfo.major" disabled></el-input>
        </el-form-item>
        <el-form-item label="宿舍编号">
          <el-input v-model="studentInfo.dorm_id" disabled></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="studentInfo.contact" disabled></el-input>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      studentInfo: {
        studentId: '',
        name: '',
        gender: '',
        major: '',
        dorm_id: '',
        contact: '',
      },
    };
  },
  methods: {
    async fetchStudentInfo() {
      const token = localStorage.getItem('token');
      if (!token) {
        this.$message.error('未登录');
        this.$router.push('/login');
        return;
      }

      try {
        const response = await axios.get('http://localhost:3000/studentInfo', {
          headers: {
            Authorization: token,
          },
        });
        this.studentInfo = response.data;
      } catch (error) {
        this.$message.error('无法获取学生信息');
      }
    },
  },
  mounted() {
    this.fetchStudentInfo();
  },
};
</script>

<style scoped>
.student-home {
  padding: 20px;
}
.student-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
}
.student-info-form {
  max-width: 600px;
  margin: 0 auto;
}
.el-input {
  height: 40px;
  line-height: 40px;
}
</style>
